package cn.nfwx.tv.manager;

import android.content.Context;
import android.text.TextUtils;

import com.chan.net.DefaultObserver;
import com.chan.net.HttpCallback;
import com.chan.net.Result;
import com.trello.rxlifecycle2.LifecycleTransformer;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.nfwx.tv.callback.ResponseCallback;
import cn.nfwx.tv.callback.ResponseListener;
import cn.nfwx.tv.callback.ResponseWithFailListener;
import cn.nfwx.tv.callback.UserCallBack;
import cn.nfwx.tv.conts.Constants;
import cn.nfwx.tv.helper.AccountHelper;
import cn.nfwx.tv.helper.UmengPushHelper;
import cn.nfwx.tv.okhttp.CustomOkHttpClient;
import cn.nfwx.tv.okhttp.Http;
import cn.nfwx.tv.okhttp.UrlHelper;
import cn.nfwx.tv.util.AppUtil;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okio.ByteString;
import retrofit2.http.GET;
import retrofit2.http.Query;

/**
 * Created by dell on 2017/11/15.
 */

public class UserManager {
    private static String login_url = Constants.test_url + "/nfwx-user/v1/user/login?";
    private static String sms_url = Constants.test_url + "/nfwx-user/v1/smscode?";
    private static String register_url = Constants.test_url + "/nfwx-user/v1/user?";
    private static String forgetpw_url = Constants.test_url + "/nfwx-user/v1/user/password/reset?";
    private static String bindphone_url = Constants.test_url + "/nfwx-user/v1/user/phone?";
    private static String refreshtoken_url = Constants.test_url + "/nfwx-user/v1/user/token?";
    private static String push_url = Constants.test_url + "/nfwx-user/v1/user/pushid";
    private static String nick_url = Constants.test_url + "/nfwx-user/v1/user/nickname";
    private static String checkphone_url = Constants.test_url + "/nfwx-user/v1/user";
    private static String validate_url = Constants.test_url + "/nfwx-user/v1/smscode/validation?";


    /***
     * 用户登录
     * @param context
     * @param mobileNo
     * @param userPasswd
     * @param callBack
     */
    public static void login(Context context, String mobileNo, String userPasswd, UserCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("method", "phone");
        params.put("phone", mobileNo);
        params.put("password", userPasswd);
        CustomOkHttpClient.postObjectRequest(context, login_url, params, Constants.TIMEOUT, "Login", callBack);

    }

    /***
     * 获取手机验证码
     * @param phone
     * @param type
     * @param callBack
     */
    public static void requestSmsCode(Context context, String phone, String type, ResponseWithFailListener<JSONObject> callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("phone", phone);
        params.put("usage", type);
        CustomOkHttpClient.postObjectRequest(context, sms_url, params, Constants.TIMEOUT, "getSmsCode", callBack);

    }

    /**
     * 用户注册
     *
     * @param context
     * @param phone
     * @param nickName
     * @param smsCode
     * @param password
     * @param callBack
     */
    public static void register(Context context, String method, String phone, String nickName, String smsCode, String password, UserCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("method", method);
        params.put("phone", phone);
        params.put("nickName", nickName);
        params.put("sms", smsCode);
        params.put("password", password);
        CustomOkHttpClient.postObjectRequest(context, register_url, params, Constants.TIMEOUT, "register", callBack);
    }

    /***
     *短信验证码登录
     * @param context
     * @param phone
     * @param smsCode
     * @param callBack
     */
    public static void loginBySms(Context context, String phone, String smsCode, UserCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("method", "sms");
        params.put("phone", phone);
        params.put("sms", smsCode);
        CustomOkHttpClient.postObjectRequest(context, login_url, params, Constants.TIMEOUT, "loginBySms", callBack);
    }

    /***
     * 微信第三方登录
     * @param context
     * @param code
     * @param callBack
     */
    public static void loginWeChat(Context context, String code, UserCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("method", "wechat");
        params.put("authCode", code);
        CustomOkHttpClient.postObjectRequest(context, register_url, params, Constants.TIMEOUT, "loginByWechat", callBack);
    }

    public static void changeUserPasswd(Context context, String phone, String smsCode, String passWord, ResponseWithFailListener<JSONObject> callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("phone", phone);
        params.put("sms", smsCode);
        params.put("password", passWord);
        CustomOkHttpClient.postObjectRequest(context, forgetpw_url, params, Constants.TIMEOUT, "changeUserPasswd", callBack);
    }

    public static void bindPhone(Context context, String phone, String smsCode, String passWord, String header, UserCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("phone", phone);
        params.put("sms", smsCode);
        if (!TextUtils.isEmpty(passWord)) {
            params.put("password", passWord);
        }
        params.put("join", "true");
        CustomOkHttpClient.putObjectRequestWithHeader(context, bindphone_url, params, Constants.TIMEOUT, "bindPhone", header, callBack);
    }

    public static void refreshToken(Context context, String refreshToken, String header, ResponseListener callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("grant_type", "refresh_token");
        params.put("refresh_token", refreshToken);
        CustomOkHttpClient.postObjectRequestWithHeader(context, refreshtoken_url, params, Constants.TIMEOUT, "refreshToken", header, callBack);

    }

    public static void upPushId(Context context, String deviceId, String header, ResponseListener callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("umeng", deviceId);
        CustomOkHttpClient.postObjectRequestWithHeader(context, push_url, params, Constants.TIMEOUT, "upPushId", header, callBack);
    }

    public static void changeNickName(Context context, String nickName, String header, UserCallBack callBack) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("nickName", nickName);
        CustomOkHttpClient.putObjectRequestWithHeader(context, nick_url, params, Constants.TIMEOUT, "changeNickName", header, callBack);
    }

    public static void checkPhone(Context context, String phoneNo, ResponseWithFailListener<String> callBack) {

        CustomOkHttpClient.getObjectRequest(context, checkphone_url + "/" + phoneNo + "/nickname?", "checkPhone", callBack);
    }

    public static void validation(Context context, String phone, String usage, String sms, ResponseWithFailListener<String> callBack) {

        CustomOkHttpClient.getObjectRequest(context, validate_url + "phone=" + phone + "&usage=" + usage + "&sms=" + sms, "validation", callBack);
    }


    /**
     * 更新昵称
     *
     * @param context
     * @param nickName
     * @param responseWithFailListener
     * @return
     */
    public static String requestIjkchangeNick(Context context, String nickName, ResponseWithFailListener<String> responseWithFailListener) {
        String request_tag = "requestIjkchangeNick";
        String url = UrlHelper.getIjkchangeNickUrl(AccountHelper.getInstance().getPhone(context), nickName);
        CustomOkHttpClient.getObjectRequest(context, url, request_tag, responseWithFailListener);
        return request_tag;
    }

    /**
     * 登录
     *
     * @param context
     * @param phone
     * @param pwd
     * @param responseListener
     * @return
     */
    public static String requestIjkLogin(Context context, String phone, String pwd, ResponseListener responseListener) {
        String request_tag = "changeNickName";
        CustomOkHttpClient.cancleAll(request_tag);
        Map<String, String> params = new HashMap<String, String>();
        params.put("mobile", phone);
        params.put("password", pwd);
        String url = UrlHelper.getIjkLoginUrl();
        CustomOkHttpClient.postObjectRequest(context, url, params, request_tag, responseListener);
        return request_tag;
    }

    /**
     * @param context
     * @param phone
     * @param responseCallback
     * @return
     */
    public static String requestIjkCheckPhone(Context context, String phone, ResponseCallback responseCallback) {
        String request_tag = "requestIjkCheckPhone";
        CustomOkHttpClient.cancleAll(request_tag);
        String url = UrlHelper.getIjkCheckPhoneUrl(phone);
        CustomOkHttpClient.getObjectRequest(context, url, request_tag, responseCallback);
        return request_tag;
    }

    /**
     * 检查微信是否有注册
     *
     * @param context
     * @param code
     * @param responseCallback
     * @return
     */
    public static String requestIjkCheckWX(Context context, String code, ResponseCallback responseCallback) {
        String request_tag = "requestIjkCheckWX";
        CustomOkHttpClient.cancleAll(request_tag);
        String url = UrlHelper.getIjkCheckWxUrl(code);
        CustomOkHttpClient.getObjectRequest(context, url, request_tag, responseCallback);
        return request_tag;
    }

    /**
     * 获取验证码
     *
     * @param context
     * @param phone
     * @param responseListener
     * @return
     */
    public static String requestIjkSmsCode(Context context, String phone, ResponseCallback responseListener) {
        String request_tag = "requestIjkSmsCode";
        CustomOkHttpClient.cancleAll(request_tag);
        String url = UrlHelper.getIjkSmsCodeUrl(phone);
        CustomOkHttpClient.getObjectRequest(context, url, request_tag, responseListener);
        return request_tag;
    }

    /**
     * 检查验证码
     *
     * @param context
     * @param phone
     * @param sms
     * @param responseListener
     * @return
     */
    public static String requestIjkCheckSms(Context context, String phone, String sms, ResponseCallback responseListener) {
        String request_tag = "requestIjkCheckSms";
        CustomOkHttpClient.cancleAll(request_tag);
        String url = UrlHelper.getIjkCheckSmsUrl(phone, sms);
        CustomOkHttpClient.getObjectRequest(context, url, request_tag, responseListener);
        return request_tag;
    }

    /**
     * 注册
     *
     * @param context
     * @param phone
     * @param nick
     * @param password
     * @param code
     * @param listener
     * @return
     */
    public static String requestIjkRegister(Context context, String phone, String nick, String password, String code, ResponseListener listener) {
        String request_tag = "requestIjkRegister";
        CustomOkHttpClient.cancleAll(request_tag);
        String url = UrlHelper.getIjkRegisterUrl();
        Map<String, String> map = new HashMap();
        map.put("mobile", phone);
        if (!TextUtils.isEmpty(nick)) {
            map.put("nick", nick);
        }
        if (!TextUtils.isEmpty(password)) {
            map.put("password", password);
        }
        map.put("deviceid", UmengPushHelper.getInstance().getDeviceToken());
        map.put("code", code);
        CustomOkHttpClient.postObjectRequest(context, url, map, request_tag, listener);
        return request_tag;
    }

    /**
     * 修改密码
     *
     * @param context
     * @param phone
     * @param pwd
     * @param responseListener
     * @return
     */
    public static String requestIjkResetPwd(Context context, String phone, String pwd, String code, ResponseListener responseListener) {
        String request_tag = "requestIjkResetPwd";
        CustomOkHttpClient.cancleAll(request_tag);
        String url = UrlHelper.getIjkResetPwd(phone, pwd, code);
        CustomOkHttpClient.getObjectRequestWithHeader(context, url, request_tag, AccountHelper.getInstance().getToken(context), responseListener);
        return request_tag;
    }

    /**
     * 绑定手机
     *
     * @return
     */
    public static String requestIjkBindPhone(Context context, String phone, String wxId, String pwd, String code, ResponseListener responseListener) {
        String request_tag = "requestIjkBindPhone";
        CustomOkHttpClient.cancleAll(request_tag);
        String url = UrlHelper.getIjkBindPhoneUrl();
        Map<String, String> map = new HashMap();
        map.put("mobile", phone);
        map.put("wxid", wxId);
        if (!TextUtils.isEmpty(pwd)) {
            map.put("password", pwd);
        }
        map.put("deviceid", UmengPushHelper.getInstance().getDeviceToken());
        map.put("code", code);
        CustomOkHttpClient.postObjectRequest(context, url, map, request_tag, responseListener);
        return request_tag;
    }

    /**
     * 公交认证
     *
     * @return
     */
    public static String requestBusAuth(Context context, String phone, String token, ResponseWithFailListener<String> responseWithFailListener) {
        String request_tag = "requestBusAuth";
        CustomOkHttpClient.cancleAll(request_tag);
        String url = UrlHelper.getBusAuthUrl(phone, token);
        CustomOkHttpClient.getObjectRequest(context, url, request_tag, responseWithFailListener);
        return request_tag;
    }

    /**
     * 更新认证
     *
     * @return
     */
    public static String requestRefreshBusAuth(Context context, String mac, ResponseWithFailListener<String> responseWithFailListener) {
        String request_tag = "requestRefreshBusAuth";
        CustomOkHttpClient.cancleAll(request_tag);
        if (TextUtils.isEmpty(mac)) {
            mac = AppUtil.getLocalMacAddressFromIp();
        }
        String url = UrlHelper.getRefreshAuthUrl("aabbccddeafeafadfad", "keepalive", mac);
        CustomOkHttpClient.getObjectRequest(context, url, request_tag, responseWithFailListener);
        return request_tag;
    }

    /**
     * 进入公交
     *
     * @return
     */
    public static String requestQpopBus(Context context, ResponseWithFailListener<String> responseWithFailListener) {
        String request_tag = "requestQpopBus";
        CustomOkHttpClient.cancleAll(request_tag);
        String url = UrlHelper.getQpopBusUrl(AccountHelper.getInstance().getUid(context));
        CustomOkHttpClient.getObjectRequestWithHeader(context, url, request_tag, AccountHelper.getInstance().getToken(context), responseWithFailListener);
        return request_tag;
    }

    /**
     * 获取IP地址
     *
     * @param context
     * @param responseWithFailListener
     * @return
     */
    public static String requestIp(Context context, ResponseWithFailListener<String> responseWithFailListener) {
        String request_tag = "requestIp";
        CustomOkHttpClient.cancleAll(request_tag);
        String url = UrlHelper.getIpUrl();
        CustomOkHttpClient.getObjectRequestWithHeader(context, url, request_tag, "", responseWithFailListener);
        return request_tag;
    }

    /**
     * 检查验证码
     *
     * @param lifecycleTransformer
     * @param mobile
     * @param chkpic
     * @param httpCallback
     * @return
     */
    public static String chkpic(LifecycleTransformer<Result<String>> lifecycleTransformer, String mobile, String chkpic, HttpCallback httpCallback) {
        String request_tag = "chkpic" + System.currentTimeMillis();
        request_tag = ByteString.encodeUtf8(request_tag).md5().hex();
        UserApi api = Http.getInstance().getCommentRetrofit(UserApi.class);
        Observable<Result<String>> observable = api.chkpic(mobile, chkpic);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(lifecycleTransformer)
                .subscribe(new DefaultObserver(request_tag, httpCallback));
        return request_tag;
    }


    private interface UserApi {
        @GET("api/sendtext_v2")
        Observable<Result<String>> chkpic(@Query("mobile") String mobile, @Query("chkpic") String chkpic);
    }
}
